﻿using System;
using ContractManager.BLL.Business;
using ContractManager.Common.Class;
using ContractManager.Common.FormBase;
using ContractManager.Entity;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ContractManager.Lists.Forms
{
    public partial class FrmAttachDocumentWorksDetail : XtraFormBase
    {
        #region "Private Variable"
        
        public bool SaveClose;
        public bool SaveNew;
        public bool OnlyView;
        public int MasterId;

        #endregion

        #region "Form Construction"
        public FrmAttachDocumentWorksDetail()
        {
            InitializeComponent();
        }
        #endregion

        #region "Private Method"

        private void BlankForm()
        {
            /*txtWorkId.EditValue = null;
            txtAttDocId.EditValue = null;
            txtAttDocWorkDescription.EditValue = null;*/
            this.ResetControls();
            txtWorkId.Properties.ReadOnly = MasterId > 0;
            txtWorkId.Properties.DataSource = ClsWorkType.SelectAll();
            txtWorkId.Properties.ValueMember = "WorkId";
            txtWorkId.Properties.DisplayMember = "WorkName";
            txtAttDocId.Properties.DataSource = ClsAttachedDocuments.SelectAll();
            txtAttDocId.Properties.ValueMember = "AttDocId";
            txtAttDocId.Properties.DisplayMember = "AttDocName";
        }

        private void GetDetail()
        {
            var detail = ClsAttachDocumentWorks.SelectByPk(MasterId);
            if (detail != null)
            {
                txtWorkId.EditValue = detail.WorkId;
                txtAttDocId.EditValue = detail.AttDocId;
                txtAttDocWorkDescription.EditValue = String.Format("{0}", detail.AttDocWorkDescription);

                txtCustomField1.EditValue = String.Format("{0}", detail.CustomField1);
                txtCustomField2.EditValue = String.Format("{0}", detail.CustomField2);
                txtCustomField3.EditValue = String.Format("{0}", detail.CustomField3);
                txtCustomField4.EditValue = String.Format("{0}", detail.CustomField4);
                txtCustomField5.EditValue = String.Format("{0}", detail.CustomField5);

            }   
        }

        private void DoSave()
        {
            #region "Check Condition"
            //if (!FormIsNotValidate)
            //{
            //    if (!dxValidationProvider.Validate())
            //    {
            //        XtraMessageBox.Show(
            //                    "Vui lòng nhập đầy đủ thông tin trước khi lưu",
            //                    "Thông báo",
            //                    MessageBoxButtons.OK,
            //                    MessageBoxIcon.Error);
            //        txtCode.SelectAll();
            //        txtCode.Focus();
            //        return;
            //    }
            //}
            #endregion

            AttachDocumentWorks item = MasterId <= 0 ? new AttachDocumentWorks() : ClsAttachDocumentWorks.SelectByPk(MasterId);
            item.AttDocWorkId = MasterId;
            if (txtWorkId.EditValue != null)
            {
                item.WorkId = Convert.ToInt32(txtWorkId.EditValue);
            }
            if (txtAttDocId.EditValue != null)
            {
                item.AttDocId = Convert.ToInt32(txtAttDocId.EditValue);
            }
            
            item.AttDocWorkDescription = String.Format("{0}", txtAttDocWorkDescription.EditValue);
            item.CustomField1 = String.Format("{0}", txtCustomField1.EditValue);
            item.CustomField2 = String.Format("{0}", txtCustomField2.EditValue);
            item.CustomField3 = String.Format("{0}", txtCustomField3.EditValue);
            item.CustomField4 = String.Format("{0}", txtCustomField4.EditValue);
            item.CustomField5 = String.Format("{0}", txtCustomField5.EditValue);

            try
            {
                if (MasterId > 0)
                {
                    ClsAttachDocumentWorks.Update(item); 
                }
                else
                {
                    ClsAttachDocumentWorks.Insert(item);
                    MasterId = item.AttDocWorkId;
                }
                if (SaveNew)
                {
                    MasterId = 0;
                    BlankForm();
                    txtWorkId.Focus();
                }
                else if (SaveClose)
                {
                    Close();
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                    String.Format("Có lỗi xảy ra trong quá trình cập nhật dữ liệu. {0}", ex.Message),
                    "Thông báo",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private void DoDelete()
        {
            if (MasterId <= 0) return;
            
            try
            {
                if (
                        XtraMessageBox.Show(
                                        "Bạn có muốn xóa bản ghi này không?",
                                        "Thông báo",
                                        MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question) == DialogResult.No) return;
                ClsAttachDocumentWorks.DeleteByPk(MasterId);
                Close();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(
                       "Có lỗi xảy ra trong quá trình xóa dữ liệu. " + ex.Message,
                       "Thông báo",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
            }
        }

        #endregion

        #region "Form Event"
        private void FrmAttachDocumentWorksDetailLoad(object sender, EventArgs e)
        {
            BlankForm();
            if (MasterId > 0) GetDetail();
            btnSaveNew.Enabled = !OnlyView;
            btnSave.Enabled = !OnlyView;
            btnSaveClose.Enabled = !OnlyView;
            btnDelete.Enabled = !OnlyView;
            btnDesign.Enabled = !OnlyView;
            if (!String.IsNullOrEmpty(FormXtraLayoutTemplate))
            {
                var fileLayout = ClsVariable.GetRestoreLayoutFullPath(FormXtraLayoutTemplate);
                layoutControl.RestoreLayoutFromXml(fileLayout);
            }
        }
        #endregion

        #region "Toolbar Event"
        private void TbMenuItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "btnSave":
                    DoSave();
                    break;
                case "btnSaveNew"://Lưu và thêm mới
                    SaveNew = true;
                    DoSave();
                    break;
                case "btnSaveClose"://Lưu và đóng
                    SaveClose = true;
                    DoSave();
                    break;
                case "btnDelete":
                    DoDelete();
                    break;
                case "btnDesign":
                    break;
                case "btnRefresh"://Nạp lại danh mục
                    break;
                case "btnHistory"://History
                    break;
                case "btnClose":
                    Close();
                    break;
            }
        }
        #endregion
    }
}
